//
//  LoiToggleStyle.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 26.03.25.
//

import SwiftUI

struct KoiToggleStyle: ToggleStyle {
    
    var onColor: Color
    var offColor: Color
    var thumbColor: Color

    func makeBody(configuration: Self.Configuration) -> some View {
        HStack {
            RoundedRectangle(cornerRadius: 20, style: .circular)
                .fill(configuration.isOn ? onColor : offColor)
                .frame(width: 24, height: 14)
                .overlay(
                    Circle()
                        .fill(thumbColor)
                        .padding(2)
                        .offset(x: configuration.isOn ? 5 : -5)
                )
                .onTapGesture {
                    withAnimation(.smooth(duration: 0.2)) {
                        configuration.isOn.toggle()
                    }
                }
        }
    }
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var isOn = false
    Toggle("开关",isOn: $isOn).toggleStyle(KoiToggleStyle(onColor: Color.koiColorPrimary, offColor: Color.koiColorDivider, thumbColor: Color.white))
}
